#!/usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import string

class VibrateParser:
    errorC = 0
    failC = 0
    def _parseLine(self,line):
        datas = string.split(line,",")
        list = []
        for data in datas:
            if "xrange" in data:
                str = string.replace(data,"xrange =","")
                str = string.strip(str)
                list.append(str)
            elif "yrange" in data:
                str = string.replace(data,"yrange =","")
                str = string.strip(str)
                list.append(str)
            elif "zrange" in data:
                str = string.replace(data,"zrange =","")
                str = string.strip(str)
                list.append(str)
        return list

    def parse(self,filepath):
        self.data = []
        if isinstance(filepath,file):
            dataFile = filepath
        else:
            dataFile = open(filepath,"r")
        for line in dataFile:
            list = self._parseLine(line)
            if len(list) ==3:
                self.data.append(list)

    def analysis(self,x=0,y=0,z=0):
        for list in self.data:
            lengh = len(list)
            if lengh != 3:
                self.errorC +=  1
                print list
                continue
            if (float(list[0]) < x or float(list[1]) < y or float(list[2] < z)):
                self.failC += 1
                print list
                continue

    def getDataCount(self):
        return len(self.data)

    def getFailCount(self):
        return self.failC
    def getErrorCount(self):
        return self.errorC

    def getFailRate(self):
        return round(float(self.failC) / (len(self.data) - self.errorC),3)

parser = argparse.ArgumentParser()
parser.add_argument("-x",help="x轴数据",dest="xrange",type=float)
parser.add_argument("-y",help="y轴数据",dest="yrange",type=float)
parser.add_argument("-z",help="z轴数据",dest="zrange",type=float)
parser.add_argument("-f",help="请输入要解析的文件",dest="file",type=file,required=True)
args = parser.parse_args()

x = 0
y = 0
z = 0
if args.xrange:
    x = args.xrange
if args.yrange:
    y = args.yrange
if args.zrange:
    z = args.zrange

vp = VibrateParser()
vp.parse(args.file)
vp.analysis(x,y,z)

print "-------------结果由此往下看---------------------------"
print "数据解析异常次数："+ str(vp.getErrorCount())
print "失败次数："+ str(vp.getFailCount())
print "总测试次数：" + str(vp.getDataCount())
print "失败比例：" + str(vp.getFailRate())
